package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.dto.device.DeviceDTO;
import com.dashu.core.dto.device.DeviceListDTO;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.service.DeviceService;
import com.dashu.core.vo.device.DeviceListVO;
import com.dashu.core.vo.device.DeviceVO;
import com.dashu.core.vo.UpdateEntityVO;
import com.dashu.core.vo.device.UpDeviceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode(ReplRyCode.SUCCESS.getCode(/ReplRyCode.SUCCESS.getCode(7 ReplRyCode.SUCCESS.getCode(5:2ReplRyCode.SUCCESS.getCode(
 */

@RequestMapping("dashu/device")
@RestController
@Slf4j
@Api(value = "dashu/device", tags = {"设备模块"}, description = "后台")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/add")
    @ApiOperation(value = "设备添加", notes = "设备添加")
    public ResultBean<DeviceDTO> add(@RequestBody @Validated DeviceVO deviceVO) throws Exception {
        deviceService.existByDeviceName(deviceVO);
        if (deviceVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "设备添加", deviceService.add(deviceVO));
    }

    @PostMapping("/updata")
    @ApiOperation(value = "设备修改", notes = "设备修改")
    public ResultBean<DeviceDTO> updata(@RequestBody @Validated UpdateEntityVO<UpDeviceVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        DeviceDTO deviceDTO = deviceService.updataByDeviceId(updateEntityVO);
        if (deviceDTO != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "设备修改", deviceDTO);
        }
        return new ResultBean<>(ReplRyCode.PERMISSION_NOT_DATA.getCode(), ReplRyCode.PERMISSION_NOT_DATA.getMessage(), deviceDTO);
    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询设备列表", notes = "父节点查询设备列表")
    public ResultBean<List<DeviceDTO>> findBase(@RequestBody @Validated PageEnty<Long> baseVo) {
        if (baseVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询设备列表", deviceService.findByBaseId(baseVo));

    }

    @GetMapping("/findBy")
    @ApiOperation(value = "设备查询", notes = "设备查询")
    public ResultBean<DeviceDTO> findBy(Long Id) {
        if (Id != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "设备查询", deviceService.findByDeviceId(Id));
        }
        return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
    }

    @GetMapping("/findByName")
    @ApiOperation(value = "设备名称查询", notes = "设备名称查询")
    public ResultBean<DeviceDTO> findName(String Name) {
        if (Name == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "权功名称查询", deviceService.DeviceName(Name));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "设备删除", notes = "设备删除")
    public ResultBean<Boolean> delete(Long Id) {
        if (Id != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "设备删除", deviceService.deleteByDeviceId(Id));
        }
        return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
    }

    @PostMapping("/findList")
    @ApiOperation(value = "设备列表", notes = "设备列表")
    public ResultBean<PageDTO<DeviceListDTO>> findList(@RequestBody @Validated PageEnty<DeviceListVO> deviceListPageEntyVO) throws Exception {
        PageDTO<DeviceListDTO> pageDTO = new PageDTO<>();
        if (deviceListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = deviceService.deviceCount(deviceListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(deviceService.findByDeviceList(deviceListPageEntyVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "设备列表", pageDTO);

    }


}
